package data_structure.video;

import java.util.Scanner;

class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入命令: ");
            System.out.println("1.显示队列");
            System.out.println("2.退出程序");
            System.out.println("3.添加数据");
            System.out.println("4.队列取出数据");
            System.out.println("5.查看第一个元素");
            int i = scanner.nextInt();
            switch (i){
                case 1:arrayQueue.showQueue();break;
                case 2:break;
                case 3:
                    System.out.println("请输入一个数: ");
                    int i1 = scanner.nextInt();
                    arrayQueue.addQueue(i1);
                    break;
                case 4:try{
                    System.out.println(arrayQueue.getQueue());
                }catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                        break;
                case 5:try {
                    System.out.println(arrayQueue.headQueue());
                }catch (Exception e){
                    System.out.println(e.getMessage());
                }
                break;
                default:break;
            }
        }
    }
}

class ArrayQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    public ArrayQueue(int maxSize) {
        //初始化数组
        arr = new int[maxSize];
        front = -1;
        rear = -1;
        this.maxSize = maxSize;
    }

    //判断队列是否满
    public boolean isFull(){
        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n){
        //判断队列是否已满
        if (isFull()){
            System.out.println("队列已满，不能加入数据...");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    //获取队列数据，出队列
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("数组为空，不能取出");
        }
        ++front;
        return arr[front];
    }

    //显示队列所有的数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空，无法遍历");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    //显示 队列的头数据
    public int headQueue(){
        if (isEmpty()){
            System.out.println("队列空，没有数据");
            throw new RuntimeException("队列空，无数据");
        }
        return arr[front+1];
    }
}
